package com.my.sl.emfreosurce.main;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import com.my.sl.emfresource.webpage.MyWeb;
import com.my.sl.emfresource.webpage.Webpage;
import com.my.sl.emfresource.webpage.WebpagePackage;


public class LoadingEmfModel {

   public static void main(String[] args){
   // Loading the existing model
      MyWeb myWeb = load();
      // Accessing the model information
      System.out.println(myWeb.getDescription());
      System.out.println(myWeb.getTitle());
      // Lets see what info the webpage has
      for (Iterator<Webpage> iterator = myWeb.getPages().iterator(); iterator
          .hasNext();) {
        Webpage page = iterator.next();
        System.out.println("Name : " + page.getName());
        // We could also iterate over the Articles...
   }
      }
   public static MyWeb load() {
      // Initialize the model
      WebpagePackage.eINSTANCE.eClass();
      
      // Register the XMI resource factory for the .website extension

      Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
      Map<String, Object> m = reg.getExtensionToFactoryMap();
      m.put("myweb", new XMIResourceFactoryImpl());

      // Obtain a new resource set
      ResourceSet resSet = new ResourceSetImpl();

      // Get the resource
      Resource resource = resSet.getResource(URI
          .createURI("website/My.myweb"), true);
      // Get the first model element and cast it to the right type, in my
      // example everything is hierarchical included in this first node
      MyWeb myWeb = (MyWeb) resource.getContents().get(0);
      return myWeb;
    }
}
